package com.twotiger.and.activity.account.repayplan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.adapter.DealPlanAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DataList;
import com.twotiger.and.bean.ProjectRepayPlan;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.PreferencesUtils;
import com.twotiger.and.util.TimeUtils;
import com.twotiger.and.util.ViewUtils;
import com.view.pulltorefresh.MyListView;
import com.view.pulltorefresh.PullToRefreshBase;
import com.view.pulltorefresh.PullToRefreshMyListView;

/**
 * 
 * @Description: 
 * 回款计划 
 * @Author douxy
 * @Date 2015年8月12日 下午1:56:56
 */
public class RepayPlanPage extends BaseActivity implements OnClickListener {

    protected static final int GETREPAYMENTSUCCES = 0;
    protected static final int GETREPAYMENTFAIL = 1;
    private PullToRefreshMyListView deal_plan_refresh_list;
    private ArrayList list;
    private int pNum = 1;
    private String TAG_refresh = "DealPlanPage_time";
    private View no_content_rl;
    private HashMap<String, String> params;
    private TextView sub_title_center_tv;
    private ImageView sub_title_left_bt;
    private DealPlanAdapter dealPlanAdapter;
    

    private ImageView no_content_bg;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.deal_plan_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        deal_plan_refresh_list = (PullToRefreshMyListView) view.findViewById(R.id.deal_plan_refresh_list);
        sub_title_center_tv = (TextView) view.findViewById(R.id.sub_title_center_tv);
        sub_title_left_bt = (ImageView) view.findViewById(R.id.sub_title_left_bt);
        no_content_rl = (View) view.findViewById(R.id.no_content_rl);
        no_content_bg = (ImageView) view.findViewById(R.id.no_content_bg);
        list = new ArrayList<ProjectRepayPlan>();
        dealPlanAdapter = new DealPlanAdapter(RepayPlanPage.this, list);
        deal_plan_refresh_list.setAdapter(dealPlanAdapter);
        deal_plan_refresh_list.setMode(PullToRefreshBase.Mode.BOTH);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.no_content_rl:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            getrepaymentlist();
            break;
        case R.id.sub_title_left_bt:
            onBackPress();
            break;
        default:
            break;
        }
    }

    @Override
    protected void setListener() {

        handler = new BaseHandler(this) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETREPAYMENTSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        DataList data = JSONObject.parseObject(result.data, DataList.class);
                        application.setRepayment_timestamp(data.getTimestamp());
                        List<ProjectRepayPlan> repayments = JSONArray.parseArray(data.getList(), ProjectRepayPlan.class);
                        if (!ListUtils.isEmpty(repayments)) {
                            no_content_rl.setVisibility(View.GONE);
                            deal_plan_refresh_list.setVisibility(View.VISIBLE);
                            if (pNum == 1) {
                                dealPlanAdapter.resetData(repayments);
                            } else {
                                dealPlanAdapter.addData(repayments);
                            }

                        } else {
                            // showToast("没有数据");
                            if(pNum == 1){
                                no_content_rl.setVisibility(View.VISIBLE);
                                no_content_bg.setBackgroundResource(R.drawable.no_content);
                                deal_plan_refresh_list.setVisibility(View.GONE);
                            }else{
                                showToast("没有更多数据");
                                dealPlanAdapter.notifyDataSetChanged();
                            }
                        }
                    } else {
                        showToast("刷新数据失败");
                    }
                    deal_plan_refresh_list.onRefreshComplete();
                    break;
                case GETREPAYMENTFAIL:
                    deal_plan_refresh_list.onRefreshComplete();
                    break;

                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
        deal_plan_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<MyListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                 * 刷新数据
                 */
                pNum = 1;
                getrepaymentlist();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<MyListView> refreshView) {
                /**
                 * 加载更多数据
                 */
                pNum++;
                getrepaymentlist();
            }
        });

        sub_title_left_bt.setOnClickListener(this);
        no_content_rl.setOnClickListener(this);
    }

    private void getrepaymentlist() {
        params.clear();
        params.put("token", getToken());
        params.put("pageNum", String.valueOf(pNum));
        params.put("pageSize", Constants.pageSize);
        params.put("type", "unRepayment");
        params.put("timestamp", TimeUtils.getTimeStamp());
        getDate(params, Constants.REPAYMENTQUERY, handler, GETREPAYMENTSUCCES, GETREPAYMENTFAIL, true, true, false);
    }

    @Override
    protected void processLogic() {
        initPage();
        initData();

    }

    public void initData() {
        getrepaymentlist();
    }

    private void initPage() {
        //        // 初始化标题
        params = getParm();
        sub_title_left_bt.setVisibility(View.VISIBLE);
        sub_title_center_tv.setText("还款计划");

    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

    }
}
